package com.xuecheng.content.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.xuecheng.base.execption.XueChengPlusException;
import com.xuecheng.content.mapper.TeachplanMapper;
import com.xuecheng.content.mapper.TeachplanMediaMapper;
import com.xuecheng.content.model.dto.BindTeachplanMediaDto;
import com.xuecheng.content.model.dto.SaveTeachplanDto;
import com.xuecheng.content.model.dto.TeachplanDto;
import com.xuecheng.content.model.po.Teachplan;
import com.xuecheng.content.model.po.TeachplanMedia;
import com.xuecheng.content.service.TeachplanService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

/**
 * @description 课程计划service接口实现类
 * @author Mr.M
 * @date 2022/9/9 11:14
 * @version 1.0
 */
@Service
public class TeachplanServiceImpl implements TeachplanService {

    @Autowired
    TeachplanMapper teachplanMapper;

    @Autowired
    TeachplanMediaMapper teachplanMediaMapper;


    /**
     * @description 查询课程计划信息
     * @param courseId  课程id
     * @return
     */
    @Override
    public List<TeachplanDto> findTeachplanTree(long courseId) {
        return teachplanMapper.selectTreeNodes(courseId);
    }




    /**
     * @description 跟新课程计划
     * @param teachplanDto  课程计划信息
     * @return void
     * @author Mr.M
     * @date 2022/9/9 13:39
     */
    @Override
    public void saveTeachplan(SaveTeachplanDto teachplanDto) {
        //判断本次操作是“添加” 还是“修改”
        Long id = teachplanDto.getId();
        if (id!=null){//“修改”
            Teachplan teachplan = new Teachplan();
            BeanUtils.copyProperties(teachplanDto,teachplan);
            teachplanMapper.updateById(teachplan);
        }else {//“添加”
            Teachplan teachplan = new Teachplan();
            BeanUtils.copyProperties(teachplanDto,teachplan);
            int i = getTeachplanCount(teachplan.getCourseId(), teachplan.getParentid());
            teachplan.setOrderby(i+1);
            teachplanMapper.insert(teachplan);
        }
    }



    /**
     * @description 教学计划绑定媒资
     * @param bindTeachplanMediaDto
     * @return com.xuecheng.content.model.po.TeachplanMedia
     * @author Mr.M
     * @date 2022/9/14 22:20
     */
    @Override
    public TeachplanMedia associationMedia(BindTeachplanMediaDto bindTeachplanMediaDto) {
        //1.删除原有记录
        //根据课程计划id查询相应课程计划
        Teachplan teachplan = teachplanMapper.selectById(bindTeachplanMediaDto.getTeachplanId());
        //如果没有查到
        if(teachplan == null){
            XueChengPlusException.cast("教学计划不存在");
        }
        //如果不是第二级标题
        if (teachplan.getGrade() != 2){
            XueChengPlusException.cast("只允许第二级教学计划绑定媒资文件");
        }
        //删除记录
        teachplanMediaMapper.delete(new LambdaQueryWrapper<TeachplanMedia>()
                .eq(TeachplanMedia::getTeachplanId,bindTeachplanMediaDto.getTeachplanId()));

        //2.添加新的记录
        TeachplanMedia teachplanMedia=new TeachplanMedia();
        //媒资文件id、课程计划id
        BeanUtils.copyProperties(bindTeachplanMediaDto,teachplanMedia);
        //文件名
        teachplanMedia.setMediaFilename(bindTeachplanMediaDto.getFileName());
        //课程id
        teachplanMedia.setCourseId(teachplan.getCourseId());
        //创建时间
        teachplanMedia.setCreateDate(LocalDateTime.now());
        //添加记录
        teachplanMediaMapper.insert(teachplanMedia);
        return teachplanMedia;
    }



    /**
     * @description 获取最新的排序号
     * @param courseId  课程id
     * @param parentId  父课程计划id
     * @return int 最新排序号
     * @author Mr.M
     * @date 2022/9/9 13:43
     */
    private int getTeachplanCount(long courseId,long parentId){
        LambdaQueryWrapper<Teachplan> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Teachplan::getCourseId,courseId);
        queryWrapper.eq(Teachplan::getParentid,parentId);
        Integer count = teachplanMapper.selectCount(queryWrapper);
        return count;
    }
}